<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * 
 * 用户登录接口
 * @author alang
 *
 */
class Api_login_mdl extends CI_Model
{
	
	const TBL_USERS = 'cj_users';
	const TBL_LOTTERY = 'cj_lottery';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 验证用户
	 * @param string $username 
	 * @param string $password
	 * @return array $user
	 */
	
	public function validate_user($email,$password)
	{
	
		$data = FALSE;
		$this->db->where('txemail',$email);
		$query = $this->db->get(self::TBL_USERS);
		if($query->num_rows()==1)
		{
			$data = $query->row_array();
		}
		
		if(!empty($data))
		{
			$data = (Common::hash_Validate($password, $data['txpassword'])) ? $data : FALSE;
			
		}
		
		$query->free_result();
		
		return $data;
	}
	
	/**
	 * 
	 * 输出
	 * @param Array $data
	 */
	
	public function outputdata($data)
	{
		
		$user = $this->validate_user($data['EMAIL'], $data['PASSWORD']);
		//print_r($user);
		if(!empty($user))
		{
			if( empty($user['active']) ){
				
				//未激活
				$outputdata['STATUS'] = 2;
				$outputdata['ERRORMESSAGE'] = ' NO ACTIVE';
				$userinfo['TOKEN'] = '';
				$userinfo['EMAIL'] = '';
				$userinfo['USERID'] = 0;
				$userinfo['LASTLOGIN']='';
				$userinfo['GOLD'] = 0;
				$userinfo['USERNAME'] = '';
				$userinfo['FACEPHOTO'] = '';
				$userinfo['ADDRESSINFO'] ='';
				
			} else {
				//激活
				$this->load->library('auth');
				$token = $this->auth->process_login($user);
				if($token)
				{
					//登录成功
					$outputdata['STATUS'] = 0;
					$outputdata['ERRORMESSAGE'] = 'SUCCEED';
					$userinfo['TOKEN'] = $token;
					$userinfo['EMAIL'] = $user['txemail'];
					$userinfo['USERID'] = (int)$user['uid'];
					$userinfo['LASTLOGIN'] = empty($user['lastlogin']) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',$user['lastlogin']);
					$userinfo['GOLD'] = empty($user['txgoldnum']) ? 0 : $user['txgoldnum'];
					$userinfo['USERNAME'] = $user['txusername'];
					$userinfo['FACEPHOTO'] = empty($user['headpic']) ? null : site_url('/upload/users/').'/'.$user['headpic'];
					$userinfo['ADDRESSINFO'] =array(
						'STATE'=>$user['state'],
						'CITY'=>$user['city'],
						'COUNTRY'=>$user['txcountry'],
						'STREET'=>$user['txaddress'],
						'ZIP'=>$user['zip'],
						'MOBILE'=>$user['mobile'],
						
					);
				}
				else 
				{
					//token失败
					$outputdata['STATUS'] = 2;
					$outputdata['ERRORMESSAGE'] = 'TOKEN ERROR';
					$userinfo['TOKEN'] = '';
					$userinfo['EMAIL'] = '';
					$userinfo['USERID'] = 0;
					$userinfo['LASTLOGIN']='';
					$userinfo['GOLD'] = 0;
					$userinfo['USERNAME'] = '';
					$userinfo['FACEPHOTO'] = '';
					$userinfo['ADDRESSINFO'] ='';
				}
			}
			
		}
		else 
		{
				$outputdata['STATUS'] = 1;
				$outputdata['ERRORMESSAGE'] = 'EMAIL OR PASSWORD ERROR';
				$userinfo['TOKEN'] = '';
				$userinfo['EMAIL'] = '';
				$userinfo['USERID'] = '';
				$userinfo['LASTLOGIN']='';
				$userinfo['GOLD'] = 0;
				$userinfo['USERNAME'] = '';
				$userinfo['FACEPHOTO'] = '';
				$userinfo['ADDRESSINFO'] ='';
		}


		$outputdata['TAG'] = $data['TAG'];
		$outputdata['DATA'] = $userinfo;
				
		header('Content-Type:text/html; charset=utf-8', false);
		echo json_encode($outputdata);
	}
	
	/**
     * 获取单个用户信息
     * 
     * @access public
	 * @param int $uid 用户id
     * @return array - 用户信息
     */
	public function get_user_by_id($id)
	{
		$data = array();
		
		$this->db->select('*')->from(self::TBL_USERS)->where('uid', $id)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
	}
	

	/**
	 * 
	 * 取得REWARD数 ...
	 * @return int num
	 */
	
	public function get_user_count()
	{
		$count = 0;
		$count =  $this->db->count_all(self::TBL_USERS);
		return $count;
	}
	
	/**
	 * 取得用户参加过未结束的活动，以及获奖情况
	 */
	
	private function get_user_join_reward($uid)
	{
		$lotteryn = $this->db->select('lottery.*,product.productname,product.id,users.txusername,users.uid')
						->from(self::TBL_LOTTERY.' as lottery')
						->join('cj_reward as reward','lottery.rewardid = reward.id')
						->join('cj_products as product','product.id = lottery.productid','left')
						->join('cj_users as users','users.uid = lottery.uid')
						->where('lottery.uid',$uid)
						->get()
						->result_array();
						
		if(!empty($lotteryn))
		{
			foreach($lotteryn as $v)
			{
				$tmp[$v['rewardid']][] = $v;
			}
		}
		print_r($tmp);
	}
	
	
}